package com.zhe.zheblogmaster.util;

import java.io.Serializable;
import java.util.StringJoiner;

/**
 * json响应数据
 *
 * @author WHAOU
 * @date 2021/8/31
 * @email whaou@qq.com
 */

public class ResultBody<T> implements Serializable {
    private static final long serialVersionUID = 15L;
    private int resultCode;
    private String message;
    private T data;

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    public static final int RESULT_CODE_SUCCESS = 200;
    public static final int RESULT_CODE_SERVER_ERROR = 500;

    public ResultBody() {
    }

    public ResultBody(int resultCode, String message, T data) {
        this.resultCode = resultCode;
        this.message = message;
        this.data = data;
    }

    public static <E> ResultBody<E> successWithoutData() {
        return new ResultBody<>(RESULT_CODE_SUCCESS, DEFAULT_SUCCESS_MESSAGE, null);
    }

    public static <E> ResultBody<E> successWithData(E data) {
        return new ResultBody<>(RESULT_CODE_SUCCESS, DEFAULT_SUCCESS_MESSAGE, data);
    }

    public static <E> ResultBody<E> error(String message) {
        return new ResultBody<>(RESULT_CODE_SERVER_ERROR, message, null);
    }

    public int getResultCode() {
        return resultCode;
    }

    public void setResultCode(int resultCode) {
        this.resultCode = resultCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", ResultBody.class.getSimpleName() + "[", "]")
                .add("resultCode=" + resultCode)
                .add("message='" + message + "'")
                .add("data=" + data)
                .toString();
    }
}
